//
//  testView.m
//  ChinaHongNiang
//
//  Created by Hancong on 15/5/20.
//  Copyright (c) 2015年 HC. All rights reserved.
//

#import "FloaterAlertView.h"
#import "HttpRequestManager.h"
#import "PayButton.h"

@interface FloaterAlertView()<UITextViewDelegate,PayButtonDelegate>
{
    BOOL _leftLeave;
}
@property (nonatomic,strong) UIView *backImageView;
@property (strong, nonatomic) IBOutlet UIView *manView;
@property (strong, nonatomic) IBOutlet UIView *womanView;
@property (strong, nonatomic) PayButton *manButton;
@property (strong, nonatomic) PayButton *womanButton;
@property (nonatomic, copy) NSString * sex;

@end
@implementation FloaterAlertView


+ (FloaterAlertView *)instanceTextView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"FloaterAlertView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        //you init
    }
    
    return self;
}

-(void)configWithLayers
{
    self.manButton = [[PayButton alloc] initWithDelegate:self groupId:@"性别选择"];
    self.manButton.frame = CGRectMake(0, 3, 45, 45);
    [self.manView addSubview:self.manButton];
    [self.manButton setChecked:YES];
    
    self.womanButton = [[PayButton alloc] initWithDelegate:self groupId:@"性别选择"];
    self.womanButton.frame = CGRectMake(0, 3, 45, 45);
    [self.womanView addSubview:self.womanButton];
    
    UIWindow *shareWindow = [[UIApplication sharedApplication]keyWindow];
    if (!self.backImageView) {
        self.backImageView = [[UIView alloc] initWithFrame:shareWindow.bounds];
    }
    self.backImageView.backgroundColor = [UIColor blackColor];
    self.backImageView.alpha = 0.5f;
    [shareWindow addSubview:self.backImageView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = self.backImageView.frame;
    btn.userInteractionEnabled = YES;
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(cancelBtn_Touch:) forControlEvents:UIControlEventTouchUpInside];
    [self.backImageView addSubview:btn];

    self.transform = CGAffineTransformMakeRotation(-M_1_PI / 2);
    CGRect afterFrame = CGRectMake(50, (CGRectGetWidth(shareWindow.bounds) - 150), 240,180);
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.transform = CGAffineTransformMakeRotation(0);
        self.frame = afterFrame;
    } completion:^(BOOL finished) {
    }];
    
    self.textView.delegate = self;
}

-(void)show
{
    UIWindow *shareWindow = [[UIApplication sharedApplication]keyWindow];
    [shareWindow addSubview:self];
    
    
}

- (IBAction)throwBtn_Touch:(id)sender {
    _leftLeave = YES;
    [self removeFromSuperview];
    if (self.throwBlock) {
        self.throwBlock();
    }
}

- (IBAction)cancelBtn_Touch:(id)sender {
    _leftLeave = NO;
    [self removeFromSuperview];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

-(void)removeFromSuperview
{
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    UIWindow *shareWindow = [[UIApplication sharedApplication]keyWindow];
    if (ScreenWidth == 320) {
        CGRect afterFrame = CGRectMake(50, ScreenHeight + 150, ScreenWidth - 100,180);
        [UIView animateWithDuration:0.45f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.frame = afterFrame;
            if (_leftLeave) {
                self.transform = CGAffineTransformMakeRotation(M_1_PI/1.5);
            }
            else
            {
                self.transform = CGAffineTransformMakeRotation(M_1_PI/1.5);
            }
        } completion:^(BOOL finished) {
            [super removeFromSuperview];
        }];
        
    } else {
        CGRect afterFrame = CGRectMake(50, ScreenHeight + 150, ScreenWidth - 100,180);
        [UIView animateWithDuration:0.45f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.frame = afterFrame;
            if (_leftLeave) {
                self.transform = CGAffineTransformMakeRotation(M_1_PI/1.5);
            }
            else
            {
                self.transform = CGAffineTransformMakeRotation(M_1_PI/1.5);
            }
        } completion:^(BOOL finished) {
            [super removeFromSuperview];
        }];
        
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}


#pragma mark - PayButtonDelegate
- (void)didSelectedRadioButton:(PayButton *)radio groupId:(NSString *)groupId {
    if (self.manButton.checked == YES) {
        NSLog(@"男");
        self.sex = @"男";
        
    }else if (self.womanButton.checked == YES) {
        NSLog(@"女");
        self.sex = @"女";
    }
}

- (void)web_requestChangeSex
{
    NSString *uidStr = [[NSUserDefaults standardUserDefaults] objectForKey:KUserDefaultsUserIdKey];
    NSString *sexStr = [NSString stringWithFormat:@"%@",self.sex];
    NSDictionary *dict =  @{@"uid" : uidStr, @"param" : @"sex", @"value" : sexStr};
    [HttpRequestManager getMethod:KChangeUserInfoInterface parameters:dict completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            if ([sexStr isEqual:@"女"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"sex" object:@"女"];
                [[NSUserDefaults standardUserDefaults] setObject:@"女" forKey:KUserDefaultKey_Sex];
                
            } else if([sexStr isEqual:@"男"]) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"sex" object:@"男"];
                [[NSUserDefaults standardUserDefaults] setObject:@"男" forKey:KUserDefaultKey_Sex];
            }
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        }else{
            NSLog(@"修改失败");
        }
    }];
}

// 确认按钮
- (IBAction)finshButton:(UIButton *)sender {
     [self web_requestChangeSex];
    [self removeFromSuperview];
    
}

// 取消按钮
- (IBAction)cancelButton:(UIButton *)sender {
    
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

